summaryrefslogtreecommitdiff
path: root/app/api/table-presets/[id]/route.ts
blob: 8146889d8fc508ec32afd4c3011f81f12a0d9a8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// app/api/table-presets/[id]/route.ts
import { NextRequest, NextResponse } from "next/server"
import { getServerSession } from "next-auth"
import { authOptions } from "@/app/api/auth/[...nextauth]/route"
import  db  from "@/db/db"
import { tablePresets } from "@/db/schema/setting"
import { eq } from "drizzle-orm"

export async function PUT(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const session = await getServerSession(authOptions)
    if (!session?.user?.id) {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
    }

    const presetId = params.id
    const body = await request.json()

    const updatedPreset = await db
      .update(tablePresets)
      .set({
        ...body,
        updatedAt: new Date(),
      })
      .where(eq(tablePresets.id, presetId))
      .returning()

    return NextResponse.json(updatedPreset[0])
  } catch (error) {
    console.error("Error updating preset:", error)
    return NextResponse.json({ error: "Internal Server Error" }, { status: 500 })
  }
}

export async function DELETE(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const session = await getServerSession(authOptions)
    if (!session?.user?.id) {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
    }

    const presetId = params.id

    await db.delete(tablePresets).where(eq(tablePresets.id, presetId))

    return NextResponse.json({ success: true })
  } catch (error) {
    console.error("Error deleting preset:", error)
    return NextResponse.json({ error: "Internal Server Error" }, { status: 500 })
  }
}